package com.blog.controller;
import com.blog.Utils.Result;
import com.blog.annotion.LoginAnnotatio;
import com.blog.base.ThreadLocalUserId;
import com.blog.bean.domain.Comment;
import com.blog.service.CommentService;
import com.blog.bean.vo.CommentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author 任浩
 * @version 1.0
 */

@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController {

    @Autowired
    private CommentService commentService;

    @PostMapping
    @LoginAnnotatio
    public Result add(@RequestBody Comment comment, HttpSession session){

        if(comment.getContent() == null || comment.getContent() == ""){
            return Result.error("不允许回复空值。");
        }

        //得到当前的用户id
        Integer userId = (Integer)session.getAttribute("userId");
        if(userId == null){
            userId = Integer.parseInt(ThreadLocalUserId.get());
        }
        //将当前的用户id，存放进入评论中
        comment.setUserId(userId);
        commentService.add(comment);

        return Result.success();
    }

    /**
     * 查询当前用户的所有回复
     * @param page
     * @param pagenum
     * @return
     */
    @GetMapping
    @LoginAnnotatio
    public Result queryCommentByUser(Integer page,Integer pagenum,HttpSession session){

        //得到当前用户的id
        Integer userId = (Integer)session.getAttribute("userId");
        if(userId == null){
            userId = Integer.parseInt(ThreadLocalUserId.get());
        }
        CommentVO commentVO = commentService.queryCommentByUser(page, pagenum, userId);
        return Result.success(commentVO);
    }
}
